﻿using AutoMapper;
using MediatR;
using S.Produce.Api.Application.Commands.WearningCommands;
using S.Produce.Domains;
using S.Produce.Infrastructure;

namespace S.Produce.Api.Application.CommandHandlers.WearningCommandHandlers
{
    /// <summary>
    /// 修改种母断奶信息
    /// </summary>
    public class WeaningUpdateCommandHandler : IRequestHandler<WeaningUpdateCommand, int>
    {
        private readonly IRepository<Weanings> _repository;
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方式注入
        /// </summary>
        /// <param name="repository"></param>
        /// <param name="mapper"></param>
        public WeaningUpdateCommandHandler(IRepository<Weanings> repository, IMapper mapper)
        {
            _repository = repository;
            this.mapper = mapper;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> Handle(WeaningUpdateCommand request, CancellationToken cancellationToken)
        {
            request.UpdateDate = DateTime.UtcNow;
            return await _repository.UpdateAsync(mapper.Map<Weanings>(request));
        }
    }
}
